package com.hckj.jianzhijia;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.avos.avoscloud.UpdatePasswordCallback;
import com.hckj.jianzhijia.controllers.RegisterController;
import com.hckj.jianzhijia.listeners.OnBackClickListener;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.ToastUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MidifyPasswordActivity extends BaseActivity {

    @InjectView(R.id.imgv_go_back)
    ImageView imgvGoBack;
    @InjectView(R.id.rl_toolbar)
    RelativeLayout rlToolbar;
    @InjectView(R.id.edt_phone_num)
    EditText edtPhoneNum;
    @InjectView(R.id.btn_get_code)
    Button btnGetCode;
    @InjectView(R.id.edt_code)
    EditText edtCode;
    @InjectView(R.id.edt_password)
    EditText edtPassword;
    @InjectView(R.id.imgv_show_password)
    ImageView imgvShowPassword;
    @InjectView(R.id.btn_register)
    Button btnRegister;
    private String phoneNum;
    private String password;
    private String messageCode;
    private boolean hasReadProtocol = true;
    private RegisterController registerController;
    private boolean passwordSeen = false;
    private CountDownTimer countDownTimer;
    private boolean ifCountDownTimerOn = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_midify_password);
        ButterKnife.inject(this);
        registerController = new RegisterController(this);
        initData();
        initEvent();
    }

    private void initData() {

            countDownTimer = new CountDownTimer(60000, 1000) {
            public void onTick(long millisUntilFinished) {
                ifCountDownTimerOn = true;
                btnGetCode.setText("倒计时" + millisUntilFinished/1000);
            }
            public void onFinish() {
                ifCountDownTimerOn = false;
                btnGetCode.setText("重新获取" );
            }
        };
    }

    private void initEvent() {
        imgvGoBack.setOnClickListener(new OnBackClickListener(this));
        imgvShowPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (passwordSeen) {//密码可见
                    edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    imgvShowPassword.setImageResource(R.drawable.looked);
                    passwordSeen = false;
                } else {
                    edtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    imgvShowPassword.setImageResource(R.drawable.look);
                    passwordSeen = true;
                }
            }
        });


        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getDataFromEdt();
                if (!registerController.checkAllData(phoneNum, password, messageCode, hasReadProtocol)) {
                    return;
                }

                AVUser.resetPasswordBySmsCodeInBackground(messageCode, password, new UpdatePasswordCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            ToastUtils.showToast(getApplicationContext(), "密码修改成功");
                            finish();

                        } else {
                            ToastUtils.showToast(getApplicationContext(), ExceptionUtils.getExceptionInfo(e));
                        }
                    }
                });
            }


        });

        btnGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(ifCountDownTimerOn){
                    return;
                }
                getDataFromEdt();
                if (!registerController.checkPhoneNum(phoneNum)) {
                    return;
                } else {

                    countDownTimer.start();

                    AVUser.requestPasswordResetBySmsCodeInBackground(phoneNum, new RequestMobileCodeCallback() {
                        @Override
                        public void done(AVException e) {
                            if (e == null) {

                            } else {
                                ToastUtils.showToast(getApplicationContext(), ExceptionUtils.getExceptionInfo(e));
                            }
                        }
                    });

//                    HashMap<String, Object> env = new HashMap<String, Object>();
//                    env.put("name", "修改密码");
//                    AVOSCloud.requestSMSCodeInBackground(phoneNum, Data.messageTemplate, env, new RequestMobileCodeCallback() {
//                        @Override
//                        public void done(AVException e) {
//                            if (e == null) {
//
//                            } else {
//                                ToastUtils.showToast(getApplicationContext(), ExceptionUtils.getExceptionInfo(e));
//                            }
//                        }
//                    });

                }
            }
        });
    }

    public void getDataFromEdt() {
        phoneNum = edtPhoneNum.getText().toString().trim();
        password = edtPassword.getText().toString().trim();
        messageCode = edtCode.getText().toString().trim();

    }
}
